package core.proto.search;

import java.util.Timer;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import core.util.logging.UserLogWriter;
import core.util.logging.constants.Action;
import core.util.logging.constants.Location;

/**
 * This class is used to write to the user log the user's query string, 
 * each time he or she types something in. Right now it works by just 
 * saving the query on each keystroke. Depending on need we might change 
 * it in the future to periodically update the user log to reduce the 
 * amount of log messages written (think of the current version as a
 * very fine-grained recorder of user actions). 
 * @author Wei Xin Yuan
 *
 */
public class QueryLoggerDocumentListener implements DocumentListener {

	private Location loc;

	public QueryLoggerDocumentListener(Location loc){
		this.loc = loc;
	}

	@Override
	public void changedUpdate(DocumentEvent de) {
		// NOOP
	}

	private void update(DocumentEvent de){
		try {
			String text = de.getDocument().getText(0, de.getDocument().getLength());
			UserLogWriter.log2(text, loc, Action.QUERY);
		} catch (BadLocationException e) {
			// Suppress. 
		}
	}

	@Override
	public void insertUpdate(DocumentEvent de) {
		update(de);
	}

	@Override
	public void removeUpdate(DocumentEvent de) {
		update(de);
	}

}
